toPositiveIntOrThrow
Returns this number as a PositiveInt, which may involve rounding or truncation, or throws IllegalArgumentException if this number is strictly negative.
var result: PositiveInt = 1.toPositiveIntOrThrow()
println(result) // 1
result = 0.toPositiveIntOrThrow()
println(result) // 0
(-1).toPositiveIntOrThrow() // IllegalArgumentException
Content copied to clipboard
You can use the toPositiveIntOrNull function for returning null
instead of throwing an IllegalArgumentException when this number is strictly negative.